package com.park.mer.service.notice.impl;

import cn.hutool.core.util.ObjectUtil;
import com.park.dao.mapper.notice.OrderNoticeMapper;
import com.park.dao.mapper.park.ParkInfoMapper;
import com.park.entity.mer.MerUserInfoLoginDto;
import com.park.entity.notice.OrderNotice;
import com.park.entity.notice.OrderNoticeVo;
import com.park.entity.park.ParkInfo;
import com.park.mer.service.notice.OrderNoticeService;
import com.park.utils.distance.LongLatDistanceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ZhangHaiWen
 * @title: OrderNoticeServiceImpl
 * @projectName park
 * @description: 商户订单通知类
 * @date 2020/5/821:00
 */
@Service(value = "orderNoticeService")
public class OrderNoticeServiceImpl implements OrderNoticeService {

    @Autowired
    private ParkInfoMapper parkInfoMapper;

    @Autowired
    private OrderNoticeMapper orderNoticeMapper;

    //查询范围内的停车场的订单
    public List<OrderNoticeVo> getNotices(MerUserInfoLoginDto merUserInfoLoginDto, Integer userId) {
        //查询范围内的停车场
        List<ParkInfo> parkInfos = parkInfoMapper.selectAll();
        Double merUserLong = merUserInfoLoginDto.getMerUserLong();
        Double merUserLat = merUserInfoLoginDto.getMerUserLat();
        List<Integer> parkIds = parkInfoList(merUserLong, merUserLat, parkInfos, 1000d);
        //查询停车场的未通知的订单
        List<OrderNoticeVo> orderNoticeVos = orderNoticeMapper.getNotices(parkIds);
        if(ObjectUtil.isEmpty(orderNoticeVos) || orderNoticeVos.size() == 0){
            orderNoticeVos = new ArrayList<OrderNoticeVo>();
        }else{
            orderNoticeMapper.updateNotice(orderNoticeVos);
        }
        return orderNoticeVos;
    }


    //查找距离范围内所有的停车场
    private List<Integer> parkInfoList(Double userLong,Double userLat,List<ParkInfo> parkInfos,double distance){
        List<Integer> parkIdlist = new ArrayList<Integer>();
        //判断是否存在可以停车的距离
        for (ParkInfo parkInfo : parkInfos) {
            Double parkLat = parkInfo.getParkLat();
            Double parkLong = parkInfo.getParkLong();
            double dis = LongLatDistanceUtil.distance(userLat, userLong, parkLat, parkLong);
            if(dis<=distance){
                parkIdlist.add(parkInfo.getId());
            }
        }
        return parkIdlist;
    }
}
